﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CuaHangDiaNhac
{
    public partial class frmQuanLyHoaDon : DevComponents.DotNetBar.Office2007Form
    {
        frmChiTietHoaDon frmChiTiet = null;
        StoreCDDataContext dt = new StoreCDDataContext();
        public frmQuanLyHoaDon()
        {
            InitializeComponent();
            XuLyDataGridView.CotCheckBox(dgrDanhSach);
        }

        private void QuanLyHoaDon_Load(object sender, EventArgs e)
        {
            BintoGrv();

        }
        void BintoGrv()
        {
            dgrDanhSach.DataSource = dt.HoaDonBan_SelectAll();
        }
        private void btnDSHoaDon_Click(object sender, EventArgs e)
        {
            BintoGrv();
        }
        DataGridViewRow r;

        void Getdata()
        {
            if (r != null)
            {
                r = dgrDanhSach.CurrentRow;

                txtNDHD.Text = ConvertType.ToString(r.Cells["ND"].Value);
                txtTenkh.Text = ConvertType.ToString(r.Cells["Tenkh"].Value);
                txtSDT.Text = ConvertType.ToString(r.Cells["SDT"].Value);
                dttNgay.Value = Convert.ToDateTime(r.Cells["Ngay"].Value);
            }
            else
            {
                r = dgrDanhSach.CurrentRow;

                Reset();
            }
        }
        private void dgrDanhSach_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            XuLyDataGridView.CheckRow(dgrDanhSach, RowSelect, e);
            r = dgrDanhSach.CurrentRow;
            Getdata();


        }
        List<int> RowSelect = new List<int>();
        private void btnXoadt_Click(object sender, EventArgs e)
        {
            foreach (int i in RowSelect)
            {
                try
                {
                    int a = Convert.ToInt32(dgrDanhSach.Rows[i].Cells["Mahd"].Value.ToString());

                    var hd = dt.HoaDonBan_Delete(a);

                    dt.SubmitChanges();
                }
                catch (Exception)
                {

                }
            }
            BintoGrv();
            //Showthem(false);
        }
        int LayMotMaHD()
        {
            int a=0;
            if(RowSelect.Count==0)
                return 0;
            foreach (int i in RowSelect)
            {
                try
                {
                    a = Convert.ToInt32(dgrDanhSach.Rows[i].Cells["Mahd"].Value.ToString());

                    var hd = dt.HoaDonBan_Delete(a);

                    dt.SubmitChanges();
                }
                catch (Exception)
                {

                }

            }
            return a;
        }
        private void dgrDanhSach_DataSourceChanged(object sender, EventArgs e)
        {
            RowSelect.Clear();
        }
        void BindtoDtb()
        {
            dt.HoaDonBan_Insert(txtNDHD.Text, Convert.ToDateTime(dttNgay.Value), txtTenkh.Text, ConvertType.ToInt(txtSDT.Text));
            dt.SubmitChanges();
        }
        private void btnThem1_Click(object sender, EventArgs e)
        {
            BindtoDtb();
            BintoGrv();
        }



        private void btnChiTiet_Click(object sender, EventArgs e)
        {
            frmMain frm = MdiParent as frmMain;
            int maso = XuLyDataGridView.GetInt(dgrDanhSach.CurrentRow, "Mahd");
            frm.btnChiTietHoaDon_Click(maso, e);
        }

        //private void txtSearch_TextChanged(object sender, EventArgs e)
        //{
        //if (cbChoose.SelectedIndex == 0)
        //    dgrDanhSach.DataSource = dt.HoaDonBan_SearchById(ConvertType.ToInt(txtSearch.Text));
        //else
        //    dgrDanhSach.DataSource = dt.HoaDonBan_SearchByDate(ConvertType.ToDate(txtSearch.Text));
        //}

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (cbChoose.SelectedIndex == 0)
                dgrDanhSach.DataSource = dt.HoaDonBan_SearchById(ConvertType.ToInt(txtSearch.Text));
            else
                dgrDanhSach.DataSource = dt.HoaDonBan_SearchByDate(ConvertType.ToDate(txtSearch.Text));
        }

        private void txtSearch_TextChanged_1(object sender, EventArgs e)
        {
            if (cbChoose.SelectedIndex == 0)
                dgrDanhSach.DataSource = dt.HoaDonBan_SearchById(ConvertType.ToInt(txtSearch.Text));
            //else
            //    dgrDanhSach.DataSource = dt.HoaDonBan_SearchByDate(ConvertType.ToDate(txtSearch.Text));
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            dt.HoaDonBan_Update(LayMotMaHD(), txtNDHD.Text, Convert.ToDateTime(dttNgay.Value), txtTenkh.Text, ConvertType.ToInt(txtSDT.Text));
            BintoGrv();
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (RowSelect.Count == 0)
                MessageBox.Show("xin vui long chon hoa don xoa.");
            else
            {
                dt.HoaDonBan_Delete(LayMotMaHD());
                BintoGrv();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Reset();
        }
        void Reset()
        {

            txtNDHD.Text = "";
            txtTenkh.Text = "";
            txtSDT.Text = "";
            dttNgay.Value = DateTime.Now;
        }
    }
}

